<?php
#### DEBUG  ####################################################################
function debug() {
	$base =& Base::getInstance();

	// For outputting debug information
	$title_debug = _('Debug Information');
	$content_debug = '<hr /><br />';

	if ($base->config['debug']['show_sql'] == TRUE) {
		// Show extended SQL information
		$title = $base->icon['info'].' '._('SQL Information');

		// Load table library
		$tbl = $base->load->library('table');
		$content = $tbl->open();
		$tbl->tr(
			$tbl->th('', 10).
			$tbl->th(_('Query')).
			$tbl->th(_('Time'), 70, TRUE).
			$tbl->th(_('Table(s)'), 150, TRUE).
			$tbl->th(_('Rows'), 50, TRUE)
		);

		if ($base->db->query_debug != NULL) {
			foreach ($base->db->query_debug as $debug) {
				$base->db->query_time += $debug['query_time'];
				$debug['query_time'] = round($debug['query_time'], 5);

				if (!isset($debug['table'])) $debug['table'] = '???';
				if (!isset($debug['rows'])) $debug['rows'] = '?';

				$stress = 0;
				if ($debug['query_time'] > 0.05) $stress++;
				if ($debug['query_time'] > 0.1) $stress++;
				if ($debug['query_time'] > 1) $stress++;

				if ($stress == 0) $color = '#ffcf26';
				elseif ($stress == 1) $color = '#ffb126';
				elseif ($stress == 2) $color = '#ff7d26';
				elseif ($stress == 3) $color = '#ff2626';

				$tbl->tr(
				$tbl->td(NULL, TRUE, NULL, NULL, 'background-color: '.$color).
				$tbl->td(htmlspecialchars($debug['query'])).
				$tbl->td($debug['query_time'], TRUE).
				$tbl->td($debug['table'], TRUE).
				$tbl->td($debug['rows'], TRUE)
				);
			}
		} else {
			$tbl->tr($tbl->td(_('There were no queries used in the generation of this page'), TRUE, 5));
		}
		$tbl->close();
		$content = $tbl->output();

		$content .= '<br /><b>Key:</b>
	<span style="color:#ffffff; background-color:#ffcf26; padding: 0 5px 0 5px;">'._('Low').'</span>
	<span style="color:#ffffff; background-color:#ffb126; padding: 0 5px 0 5px;">'._('Medium').'</span>
	<span style="color:#ffffff; background-color:#ff7d26; padding: 0 5px 0 5px;">'._('High').'</span>
	<span style="color:#ffffff; background-color:#ff2626; padding: 0 5px 0 5px;">'._('Warning').'</span><br /><br />';

		$base->load->helper('statistics');

		$base->var['generation_time'] = micro_time() - $base->var['generation_time'];

		$content .= '<b>'._('Queries').':</b> '.$base->db->query_num.'<br />
	<b>'._('Server Load').':</b> '.server_load().'<br />
	<b>'._('Server Uptime').':</b> '.server_uptime().'<br />
	<b>'._('Page Generation Time').':</b> '.round($base->var['generation_time'], 5).'<br />
	<b>'._('Query Time').':</b> '.round($base->db->query_time, 5).' ('.round(($base->db->query_time / $base->var['generation_time']) * 100, 1).'% '._('of total execution time').')<br />';

		$content = '<div id="sql_information" style="display:block;">'.$content.'</div>';

		$template = new template('box', 'common');
		$template->assign('TITLE', $title);
		$template->assign('BODY', $content);
		$content_debug .= $template->output();

	} elseif ($base->config['debug']['show_sql_simple_view'] == TRUE) {
		// Show simple SQL information
		$title_left = $icon['info'].' SQL';
		$content = NULL;

		if ($base->db->query_debug != NULL) {
			foreach ($base->db->query_debug as $debug) {
				extract($debug);
				$content .= htmlspecialchars(wordwrap($query)).'<br />';
			}
		}

		$content = '<div id="sql_information" style="display:block;">'.$content.'</div>';

		$template = new template('box', 'common');
		$template->assign('TITLE', $title);
		$template->assign('BODY', $content);
		$content_debug .= $template->output();
	}

	// Show module/block/function timers
	if (url(1) && $base->config['debug']['show_url_array'] == TRUE) {

		$content = '<pre>'.print_r(url(), TRUE).'</pre>';

		$title = $icon['info'].' '._('URL Array');
		$content = '<div id="url_array" style="display:block;">'.$content.'</div>';

		$template = new template('box', 'common');
		$template->assign('TITLE', $title);
		$template->assign('BODY', $content);
		$content_debug .= $template->output();
	}

	// Show module/block/function timers
	if ($base->config['debug']['show_generation_timers'] == TRUE) {

		$content = NULL;

		if ($module_time) {
			if ($content) $content .= '<br />';
			$content .= $icon['arrow'].' <b'._('Module Timers').':</b><br />';
			arsort($module_time);
			foreach ($module_time as $name => $time)
			$content .= '&nbsp;'.$icon['bullet'].' '.ucwords($name).' '.round($time, 4).'<br />';
		}

		if ($module_time) {
			if ($content) $content .= '<br />';
			$content .= $icon['arrow'].' <b>'._('Function Timers').':</b><br />';
			arsort($function_time);
			foreach ($function_time as $name => $time)
			$content .= '&nbsp;'.$icon['bullet'].' '.ucwords($name).' '.round($time, 4).'<br />';
		}

		$title = $icon['info'].' '._('Generation Timers');
		$content = '<div id="generation_timers" style="display:none;">'.$content.'</div>';

		$template = new template('box', 'common');
		$template->assign('TITLE', $title);
		$template->assign('BODY', $content);
		$content_debug .= $template->output();
	}

	// Show errors and warnings
	if ($base->config['debug']['show_errors'] == TRUE) {
		global $debug_errors;

		if (!$debug_errors) $debug_errors = _('There are no errors, warnings or notices');

		$title = $icon['info'].' '._('Errors, Warnings & Notices');
		$content = '<div id="debug_errors" style="display:none;">'.$debug_errors.'</div>';

		$template = new template('box', 'common');
		$template->assign('TITLE', $title);
		$template->assign('BODY', $content);
		$content_debug .= $template->output();

	}

	// Show errors and warnings
	if ($base->config['debug']['show_headers'] == TRUE) {

		$content = '<pre>'.print_r(apache_response_headers(), TRUE).'</pre>';
		$content .= '<pre>'.print_r(apache_request_headers(), TRUE).'</pre>';

		// Encase in a div with overflows
		$content = '<div style="width:100%;overflow:auto">'.$content.'</div>';

		$title = $icon['info'].' Response/Request Headers';
		$content = '<div id="debug_errors" style="display:none;">'.$content.'</div>';

		$template = new template('box', 'common');
		$template->assign('TITLE', $title);
		$template->assign('BODY', $content);
		$content_debug .= $template->output();

	}

	// Show Superglobals
	if ($base->config['debug']['show_get'] || $base->config['debug']['show_post'] || $base->config['debug']['show_file'] || $base->config['debug']['show_session'] || $base->config['debug']['show_cookie'] || $base->config['debug']['show_server'] || $base->config['debug']['show_env']) {

		$content_debug_superglobals = NULL;

		// Show _GET GLOBAL
		if ($base->config['debug']['show_get']) {
			$title = 'Get';
			$content = NULL;

			if (isset($_GET) && count($_GET) > 0) $content .= '<pre>'.print_r($_GET, TRUE).'</pre>';

			// Encase in a div with overflows
			$content = '<div style="width:100%;overflow:auto">'.$content.'</div>';

			$template = new template('box', 'common');
			$template->assign('TITLE', $title);
			$template->assign('BODY', $content);
			$content_debug_superglobals .= $template->output();
		}

		// Show _POST GLOBAL
		if ($base->config['debug']['show_post']) {
			$title = 'Post';
			$content = NULL;

			if (isset($_POST) && count($_POST) > 0) $content .= '<pre>'.print_r($_POST, TRUE).'</pre>';

			// Encase in a div with overflows
			$content = '<div style="width:100%;overflow:auto">'.$content.'</div>';

			$template = new template('box', 'common');
			$template->assign('TITLE', $title);
			$template->assign('BODY', $content);
			$content_debug_superglobals .= $template->output();
		}

		// Show _FILE GLOBAL
		if ($base->config['debug']['show_file']) {
			$title = 'File';
			$content = NULL;

			if (isset($_FILE) && count($_FILE) > 0) $content .= '<pre>'.print_r($_FILE, TRUE).'</pre>';

			// Encase in a div with overflows
			$content = '<div style="width:100%;overflow:auto">'.$content.'</div>';

			$template = new template('box', 'common');
			$template->assign('TITLE', $title);
			$template->assign('BODY', $content);
			$content_debug_superglobals .= $template->output();
		}

		// Show _SESSION GLOBAL
		if ($base->config['debug']['show_session']) {
			$title = 'Session';
			$content = NULL;

			if (isset($_SESSION) && count($_SESSION) > 0) $content .= '<pre>'.print_r($_SESSION, TRUE).'</pre>';

			// Encase in a div with overflows
			$content = '<div style="width:100%;overflow:auto">'.$content.'</div>';

			$template = new template('box', 'common');
			$template->assign('TITLE', $title);
			$template->assign('BODY', $content);
			$content_debug_superglobals .= $template->output();
		}

		// Show _COOKIE GLOBAL
		if ($base->config['debug']['show_cookie']) {
			$title = 'Cookie';
			$content = NULL;

			if (isset($_COOKIE) && count($_COOKIE) > 0) $content .= '<pre>'.print_r($_COOKIE, TRUE).'</pre>';

			// Encase in a div with overflows
			$content = '<div style="width:100%;overflow:auto">'.$content.'</div>';

			$template = new template('box', 'common');
			$template->assign('TITLE', $title);
			$template->assign('BODY', $content);
			$content_debug_superglobals .= $template->output();
		}

		// Show _SERVER GLOBAL
		if ($base->config['debug']['show_server']) {
			$title = 'Server';
			$content = NULL;

			if (isset($_SERVER) && count($_SERVER) > 0) $content .= '<pre>'.print_r($_SERVER, TRUE).'</pre>';

			// Encase in a div with overflows
			$content = '<div style="width:100%;overflow:auto">'.$content.'</div>';

			$template = new template('box', 'common');
			$template->assign('TITLE', $title);
			$template->assign('BODY', $content);
			$content_debug_superglobals .= $template->output();
		}

		// Show _ENV GLOBAL
		if ($base->config['debug']['show_env']) {
			$title = 'Env';
			$content = NULL;

			if (isset($_ENV) && count($_ENV) > 0) $content .= '<pre>'.print_r($_ENV, TRUE).'</pre>';

			// Encase in a div with overflows
			$content = '<div style="width:100%;overflow:auto">'.$content.'</div>';

			$template = new template('box', 'common');
			$template->assign('TITLE', $title);
			$template->assign('BODY', $content);
			$content_debug_superglobals .= $template->output();
		}

		$title= $base->icon['show_hide'].' '._('Superglobals');
		$content = slider('#debug_superglobals', '#debug_superglobals_toggle');
		$content .= $content_debug_superglobals;

		$template = new template('box', 'common');
		$template->assign('TITLE', '<span id="debug_superglobals_toggle">'.$title.'</span>');
		$template->assign('BODY', '<div id="debug_superglobals">'.$content.'<br /></div>');
		$content_debug .= $template->output();
	}

	// Show config array
	if ($base->config['debug']['show_config']) {
		$content = slider('#debug_config', '#debug_config_toggle');

		if (count($base->config) > 0) $content .= print_array($base->config);

		$template = new template('box', 'common');
		$template->assign('TITLE', '<span id="debug_config_toggle">'.$base->icon['show_hide'].' '._('Config Array').'</span>');
		$template->assign('BODY', '<div id="debug_config">'._('An array listing of $this->config taken from the config database table').'.<br />'.$content.'<br /></div>');

		$content_debug .= $template->output();
	}

	// Show common array
	if ($base->config['debug']['show_common']) {
		$title = $base->icon['show_hide'].' '._('Common Array');
		$content = slider('#debug_common', '#debug_common_toggle');

		if (count($base->common) > 0) $content .= print_array($base->common);

		$template = new template('box', 'common');
		$template->assign('TITLE', '<span id="debug_common_toggle">'.$title.'</span>');
		$template->assign('BODY', '<div id="debug_common">'._('An array listing of $this->common taken from the common database table').'.<br />'.$content.'<br /></div>');

		$content_debug .= $template->output();
	}


	// Show included files
	if ($base->config['debug']['show_included'] == TRUE) {
		$title = $base->icon['show_hide'].' '._('Includes');
		$content = slider('#debug_included', '#debug_included_toggle');

		$included_files = get_included_files();
		$pos = strrpos($included_files[0], '\\');

		$content .= '<h2>Includes</h2>';
		$include_count = 0;
		foreach ($included_files as $file) {
			if (!strstr($file, 'cache/') && !strstr($file, 'templates/') && !strstr($file, 'modules/') && !strstr($file, 'index.php')) { $include_count++; $content .= $include_count.'. '.substr($file, $pos).'<br />'; }
		}

		$content .= '<br /><h2>Modules</h2>';
		$total_count = $include_count;
		$include_count = 0;
		foreach ($included_files as $file) {
			if (strstr($file, 'modules/')) { $include_count++; $content .= $include_count.'. '.substr($file, $pos).'<br />'; }
		}

		$total_count += $include_count;
		$content .= '<br /><h2>Cache Files</h2>';
		$include_count = 0;
		foreach ($included_files as $file) {
			if (strstr($file, 'cache/') && $include_count == 1) $content .= '<br /><h2>PHP</h2>';
			if (strstr($file, 'cache/')) { $include_count++; $content .= $include_count.'. '.substr($file, $pos).'<br />'; }
		}

		$total_count += $include_count;
		$content .= '<br /><h2>Templates</h2>';
		$include_count = 0;
		foreach ($included_files as $file) {
			if (strstr($file, 'templates/')) { $include_count++; $content .= $include_count.'. '.substr($file, $pos).'<br />'; }
		}

		$total_count += $include_count;

		$template = new template('box', 'common');
		$template->assign('TITLE', '<span id="debug_included_toggle">'.$title.'&nbsp;('.$total_count.' files)</span>');
		$template->assign('BODY', '<div id="debug_included">'.$content.'</div><br />');

		$content_debug .= $template->output();
	}

	$template = new template('debug', 'common');
	$template->assign('TITLE', $title_debug);
	$template->assign('BODY', $content_debug);
	return $template->output();

}
?>